package com.xujianjie.goodsservice.controller;

import com.xujianjie.goodsservice.model.Goods;
import com.xujianjie.goodsservice.model.ResponseData;
import com.xujianjie.goodsservice.service.GoodsService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/goods")
public class GoodsController
{
    @Autowired
    private GoodsService goodsService;

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public ResponseData info(Integer goodsId)
    {
        Goods goods = goodsService.findById(goodsId);
        if (goods != null)
        {
            return new ResponseData(ResponseData.STATUS_OK, goods, "获取成功！");
        }
        else
        {
            return new ResponseData(ResponseData.STATUS_FAILED, null, "获取失败！");
        }
    }

    @RequestMapping(value = "/inventory/deduct", method = RequestMethod.GET)
    public ResponseData deductBalance(Integer goodsId, Integer count)
    {
        if (goodsService.deductInventory(goodsId, count))
        {
            return new ResponseData(ResponseData.STATUS_OK, null, "扣减成功！");
        }
        else
        {
            return new ResponseData(ResponseData.STATUS_FAILED, null, "扣减失败！");
        }
    }
}
